/*
----------------------------------------------------------------------------
Copyright (c) 2005 - 2009, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above
	  copyright notice, this list of conditions and the
	  following disclaimer.

    * Redistributions in binary form must reproduce the above
	  copyright notice, this list of conditions and the following
	  disclaimer in the documentation and/or other materials
	  provided with the distribution.

    * Neither the copyright holder's name nor the names of its
	  contributors may be used to endorse or promote products
	  derived from this software without specific prior written
	  permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*--------------------------- ITMemoryPool.hpp ----------------------------*/
/*===========================================================================
FILE: ITMemoryPool.hpp
Author: Wangxing SHI (swxlion)
Version: 0.0.2
Date: 2009-06-11

File Description:
    Define the interface & data structures for the memory pool series.

Dependencies:
    Platform: Platform Independent.

History:
    version        author        date       description
	----------------------------------------------------
	0.0.1          swxlion       07-04-17   Created the first version for ASDL.
	0.0.2          swxlion       09-06-11   Integrated to Luna Log.
===========================================================================*/
#ifndef _LUNA_LOG__MEMORY_POOL_DATA_STRUCTURE_H_
#define _LUNA_LOG__MEMORY_POOL_DATA_STRUCTURE_H_

/* 
** Because the new and delete operations will lead to calling the default constructor
** and destructor twice when first using the memory blocks appended to the pool and
** free the memory pool.
*/
#define _LUNA_LOG__TMEMORY_POOL__USE_MALLOC_
#include <stdlib.h>
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "Constant.hpp"
/*===============================================================================
  CLASS & STRUCTURE DEFINITIONS
=============================================================================== */
namespace LunaLog
{
	typedef struct _MemoryPoolStatus
	{
		ULONG				ulMaxNumber;
		ULONG				ulPerIncNumber;
		ULONG				ulPerfectNumber;

		ULONG				ulUsedNumber;		//-- Not include the Overdraft nodes.
		ULONG				ulFreeNumber;
		ULONG				ulOverdraftNumber;
		ULONG				ulTotalUsedNumber;
		ULONG				ulTotalNumber;
	} MemoryPoolStatus;

	template < typename K >
	class ITMemoryPool
	{
	public:
		virtual				~ITMemoryPool() {}

		virtual bool		Init( ULONG ulInitNumber = LunaLog::Constant::MemoryPool::gc_ulInitNum,
								  ULONG ulMaxNumber = LunaLog::Constant::MemoryPool::gc_ulMaxNum,
								  ULONG ulPerIncNumber = LunaLog::Constant::MemoryPool::gc_ulIncNum,
								  ULONG ulPerfectNumber = LunaLog::Constant::MemoryPool::gc_ulPerfectNum ) = 0;
		virtual void		Free() = 0;
		
		virtual K*			Gain() = 0;
		virtual void		Recycle( K** ppNode ) = 0;
		virtual void		Recycle( K* & pNode ) = 0;

		virtual bool		IsInited() = 0;

		virtual void		GetStatus( MemoryPoolStatus &strMPS ) = 0;
		virtual void		GetStatusSnapshot( MemoryPoolStatus &strMPS ) = 0;
	};

#ifdef _MSC_VER

	template < typename T >
	struct MemoryData
	{
		T							Data;						//-- Must be the first field. Facilitate for the explicit conversion.
		struct MemoryData<T>		*Pre;
		struct MemoryData<T>		*Next;
		//BOOL						bOverdraft;					//-- The compilers will align data types on a natural boundary.
		void*						bOverdraft;					//Using pointer type rather than BOOL type just for memory alignment. NULL <=> false, !NULL <=> true.

		MemoryData(): Pre(0), Next(0), bOverdraft(0)
		{
		}
	};

	template < typename T >
	struct MemoryDataLite
	{
		T							Data;						//-- Must be the first field. Facilitate for the explicit conversion.
		struct MemoryDataLite<T>	*Pre;
		struct MemoryDataLite<T>	*Next;

		MemoryDataLite(): Pre(0), Next(0)
		{
		}
	};
#else

	template < typename T >
	struct MemoryData
	{
		T							Data;						//-- Must be the first field. Facilitate for the explicit conversion.
		struct MemoryData<T>		*Pre;
		struct MemoryData<T>		*Next;
		//bool						bOverdraft;					//-- The compilers will align data types on a natural boundary.
		void*						bOverdraft;					//Using pointer type rather than bool type just for memory alignment. NULL <=> false, !NULL <=> true.

		MemoryData(): Pre(0), Next(0), bOverdraft(0)
		{
		}

#ifndef _LUNA_LOG__TMEMORY_POOL__USE_MALLOC_
		inline bool Delete( MemoryData<T>** ppHead )
		{
			if( Pre )
				if( Next )
				{
					Next->Pre = Pre;
					Pre->Next = Next;
				}
				else
					Pre->Next = NULL;
			else
			{
				if( !ppHead || !(*ppHead) )
					return false;
				else
					*ppHead = Next;

				if( Next )
				{
					Next->Pre = NULL;
				}
			}

			delete this;
			return true;
		}

		inline bool Delete( MemoryData<T>* &pHead )
		{
			if( Pre )
				if( Next )
				{
					Next->Pre = Pre;
					Pre->Next = Next;
				}
				else
					Pre->Next = NULL;
			else
			{
				if( !pHead )
					return false;
				else
					pHead = Next;

				if( Next )
				{
					Next->Pre = NULL;
				}
			}

			delete this;
			return true;
		}
#endif
	};

	template < typename T >
	struct MemoryDataLite
	{
		T							Data;						//-- Must be the first field. Facilitate for the explicit conversion.
		struct MemoryDataLite<T>	*Pre;
		struct MemoryDataLite<T>	*Next;

		MemoryDataLite(): Pre(0), Next(0)
		{
		}

#ifndef _LUNA_LOG__TMEMORY_POOL__USE_MALLOC_
		inline bool Delete( MemoryDataLite<T>** ppHead )
		{
			if( Pre )
				if( Next )
				{
					Next->Pre = Pre;
					Pre->Next = Next;
				}
				else
					Pre->Next = NULL;
			else
			{
				if( !ppHead || !(*ppHead) )
					return false;
				else
					*ppHead = Next;

				if( Next )
				{
					Next->Pre = NULL;
				}
			}

			delete this;
			return true;
		}

		inline bool Delete( MemoryDataLite<T>* &pHead )
		{
			if( Pre )
				if( Next )
				{
					Next->Pre = Pre;
					Pre->Next = Next;
				}
				else
					Pre->Next = NULL;
			else
			{
				if( !pHead )
					return false;
				else
					pHead = Next;

				if( Next )
				{
					Next->Pre = NULL;
				}
			}

			delete this;
			return true;
		}
#endif
	};
#endif
}

#endif // _LUNA_LOG__MEMORY_POOL_DATA_STRUCTURE_H_
